package com.jeff.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

/**
 * author：duff
 * version：1.0.0
 * date：2017/8/31
 * desc：
 */
public class TimeUtil {
    /**
     * 国际标准绝对时区
     */
    public static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone("UTC");
    /**
     * 本地时区
     */
    public static final TimeZone LOCAL_TIME_ZONE = TimeZone.getDefault();

    public static final SimpleDateFormat DEFAULT_DATE_TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static final SimpleDateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat("yyyy.MM.dd");

    /**
     * 以utc标准时区，格式化时间毫秒
     *
     * @param utcMilliseconds
     * @param format
     * @return 2014-10-21
     */
    public static String formatMillsUTC(long utcMilliseconds, String format) {
        return formatMills(utcMilliseconds, format, UTC_TIME_ZONE);
    }

    /**
     * 以utc标准时区，格式化时间秒，
     *
     * @param utcSeconds
     * @param format
     * @return
     */
    public static String formatSecondsUTC(long utcSeconds, String format) {
        return formatMillsUTC(TimeUnit.SECONDS.toMillis(utcSeconds), format);
    }

    public static long getUTCTime(long unixTimeInMills) {
        return unixTimeInMills + TimeZone.getDefault().getRawOffset();
    }

    /**
     * * 以本地时区为准，格式化时间毫秒，
     *
     * @param utcMilliseconds
     * @param format
     * @return
     */
    public static String formatMillsLocal(long utcMilliseconds, String format) {
        return formatMills(utcMilliseconds, format, LOCAL_TIME_ZONE);
    }

    /**
     * 以本地时区为准，格式化时间秒，
     *
     * @param utcSeconds
     * @param format
     * @return
     */
    public static String formatSecondsLocal(long utcSeconds, String format) {
        return formatMillsLocal(TimeUnit.SECONDS.toMillis(utcSeconds), format);
    }

    /**
     * 以utc标准时区，格式化时间秒，
     *
     * @param utcSeconds
     * @param format
     * @return
     */
    public static String formatSecondsDefault(long utcSeconds, String format) {
        return formatMillsDefault(TimeUnit.SECONDS.toMillis(utcSeconds), format);
    }

    public static String formatMillsDefault(long utcMilliseconds, String format) {
        return formatMills(utcMilliseconds, format, null);
    }

    /**
     * @param milliseconds
     * @param format
     * @param timeZone     可以参考{@link #UTC_TIME_ZONE}和{@link #LOCAL_TIME_ZONE},也可以自定义的TimeZone
     * @return
     */
    public static String formatMills(long milliseconds, String format, TimeZone timeZone) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format, Locale.getDefault());
        if (timeZone != null) {
            simpleDateFormat.setTimeZone(timeZone);
        }
        return simpleDateFormat.format(milliseconds);
    }

    public static String formatSeconds(long seconds, String format, TimeZone timeZone) {
        return formatMills(TimeUnit.SECONDS.toMillis(seconds), format, timeZone);
    }

    /**
     * long time to string, format is {@link #DEFAULT_DATE_TIME_FORMAT}
     *
     * @param timeInMillis
     * @return
     */
    public static String getSimpleDateTime(long timeInMillis) {
        return DEFAULT_DATE_TIME_FORMAT.format(new Date(timeInMillis));
    }

    /**
     * 获取时间格式如：2014-05-01
     */

    public static String getSimpleDate(long timeInMillis) {
        return DEFAULT_DATE_FORMAT.format(new Date(timeInMillis));
    }

    /**
     * @param seconds
     * @return 2014-10-21
     */
    public static String getTimeHourLevel(long seconds) {
        long hour = seconds / 3600;

        long minute = seconds % 3600 / 60;

        long second = seconds % 60;

        StringBuilder sb = new StringBuilder();
        if (hour > 0) {
            sb.append(hour).append("h ");
        }
        if (minute > 0) {
            sb.append(minute).append("m");
        }
        if (hour == 0) {
            sb.append(second).append("s");
        }
        return sb.toString();
    }

    public static String getCurrentYearMonthDayMinuteSecondTime() {
        Calendar c = Calendar.getInstance();
        String year = String.valueOf(c.get(Calendar.YEAR));
        String month = String.valueOf(c.get(Calendar.MONTH));
        String day = String.valueOf(c.get(Calendar.DAY_OF_MONTH) + 1);
        String hour = String.valueOf(c.get(Calendar.HOUR_OF_DAY));
        String minute = String.valueOf(c.get(Calendar.MINUTE));
        String second = String.valueOf(c.get(Calendar.SECOND));
        return (year + month + day + hour + minute + second);
    }

    /**
     * 将ms转化为分秒(00:00)
     *
     * @param milliSecs
     * @return
     */
    public static String milliSec2minSec(long milliSecs) {
        StringBuffer sb = new StringBuffer();
        long m = milliSecs / (60 * 1000);
        sb.append(m < 10 ? "0" + m : m);
        sb.append(":");
        long s = (milliSecs % (60 * 1000)) / 1000;
        sb.append(s < 10 ? "0" + s : s);
        return sb.toString();
    }

}
